//////////////////////// 函数 ////////////////////////

// function add(x,y){
//     return x+y;
// }
// let myAdd = fucntion(x,y){
//     return x+y;
// }

// let z = 100;

// function addToZ(x,y){
//     return x + y + z;
// }

//////////////////////// 函数类型 ////////////////////////

/////////////////// 为函数定义类型

// function add(x: number, y: number): number{
//     return x+y;
// }

// let myAdd = function(x: number, y: number): number {
//     return x + y;
// }

/////////////////// 书写完整函数类型

// let myAdd: (x: number, y: number) => number = 
// function(x: number, y: number): number {
//     return x + y;
// }

// let myAdd2: (baseValue: number, increment: number) => number =
// function(x: number, y: number): number {
//     return x + y;
// }

/////////////////// 推断类型

// let myAdd = function(x:number, y: number): number {
//     return x + y ;
// }

// let myAdd2: (baseValue: number, increment: number) => nubmer = 
// function(x,y){
//     return x + y;
// }

//////////////////////// 可选参数和默认参数 ////////////////////////

// function buildName(firstName: string, lastName: string){
//     return firstName + " " + lastName;
// }

// let result1 = buildName("Bob");
// let result2 = buildName("Bob","Adams","Sr.");
// let result3 = buildName("Bob","Adams");

/////////////////// 可选参数

// function buildName(firstName: string, lastName?: string){
//     if(lastName){
//         return firstName + " " + lastName;        
//     }
//     else{
//         return firstName;
//     }
// }

// let result1 = buildName("Bob");
// let result2 = buildName("Bob","Adams","Sr.");
// let result3 = buildName("Bob","Adams");

/////////////////// 参数默认值

// function buildName(firstName: string, lastName: = "Smith"){
//     return firstName + " " + lastName;
// }

// let result1 = buildName("Bob");                  // works correctly now, returns "Bob Smith"
// let result2 = buildName("Bob", undefined);       // still works, also returns "Bob Smith"
// let result3 = buildName("Bob", "Adams", "Sr.");  // error, too many parameters
// let result4 = buildName("Bob", "Adams");         // ah, just right

/////////////////// 带默认值的参数不需要放在必须参数的后面

// function buildName(firstName = "Will", lastName: string) {
//     return firstName + " " + lastName;
// }

// let result1 = buildName("Bob");                  // error, too few parameters
// let result2 = buildName("Bob", "Adams", "Sr.");  // error, too many parameters
// let result3 = buildName("Bob", "Adams");         // okay and returns "Bob Adams"
// let result4 = buildName(undefined, "Adams");     // okay and returns "Will Adams"

//////////////////////// 剩余参数 ////////////////////////

// function buildName(firstName: string, ...restOfName: string[]){
//     return firstName + " " + restOfName.join(" ");
// }

// let employeeName = buildName("Joseph","Samuel","lucas","MacKinzie")

/////////////////// 带有剩余参数的函数类型定义

// function buildName(firstName: string, ...restOfName: string[]){
//     return firstName + " " +restOfName.join(" ")
// }

// let buildNameFun: (fname: string, ...rest: string[]) => string = buildName


//////////////////////// this ////////////////////////

/////////////////// this和箭头函数

// let deck = {
//     suits: ["hearts", "spades", "clubs", "diamonds"],
//     cards: Array(52),
//     createCardPicker: function() {
//         return function() {
//             let pickedCard = Math.floor(Math.random() * 52);
//             let pickedSuit = Math.floor(pickedCard / 13);
//             // this被设置成了window而不是deck对象
//             return {suit: this.suits[pickedSuit], card: pickedCard % 13};
//         }
//     }
// }

// let cardPicker = deck.createCardPicker();
// let pickedCard = cardPicker();

// alert("card: " + pickedCard.card + " of " + pickedCard.suit);

/////////////////// 使用箭头函数修正问题

// let deck = {
//     suits: ["hearts", "spades", "clubs", "diamonds"],
//     cards: Array(52),
//     createCardPicker: function() {
//         // NOTE: the line below is now an arrow function, allowing us to capture 'this' right here
//         return () => {
//             let pickedCard = Math.floor(Math.random() * 52);
//             let pickedSuit = Math.floor(pickedCard / 13);

//             return {suit: this.suits[pickedSuit], card: pickedCard % 13};
//         }
//     }
// }

// let cardPicker = deck.createCardPicker();
// let pickedCard = cardPicker();

// alert("card: " + pickedCard.card + " of " + pickedCard.suit);

/////////////////// this参数

// interface Card{
//     suit: string;
//     card: number;
// }

// interface Deck{
//     suits: string[];
//     cards: number[];
//     createCardPicker(this: Deck): ()=> Card;
// }

// let deck: Deck = {
//     suits: ["hearts", "spades", "clubs", "diamonds"],
//     cards: Array(52),
//     // NOTE: The function now explicitly specifies that its callee must be of type Deck
//     createCardPicker: function(this: Deck) {
//         return () => {
//             let pickedCard = Math.floor(Math.random() * 52);
//             let pickedSuit = Math.floor(pickedCard / 13);

//             return {suit: this.suits[pickedSuit], card: pickedCard % 13};
//         }
//     }
// }

// let cardPicker = deck.createCardPicker();
// let pickedCard = cardPicker();

// alert("card: " + pickedCard.card + " of " + pickedCard.suit);


/////////////////// this参数在回调函数里

// class Handler {
//     info: string;
//     onClickBad(this: Handler, e: Event) {
//         // oops, used this here. using this callback would crash at runtime
//         this.info = e.message;
//     }
// }
// let h = new Handler();
// uiElement.addClickListener(h.onClickBad); // error!

// class Handler {
//     info: string | undefined;
//     onClickGood(this: void, e: Event) {
//         // can't use this here because it's of type void!
//         console.log('clicked!');
//     }
// }
// let h = new Handler();
// uiElement.addClickListener(h.onClickGood);


//////////////////////// 重载 ////////////////////////

// let suits = ["hearts", "spades", "clubs", "diamonds"];

// function pickCard(x): any {
//     // Check to see if we're working with an object/array
//     // if so, they gave us the deck and we'll pick the card
//     if (typeof x == "object") {
//         let pickedCard = Math.floor(Math.random() * x.length);
//         return pickedCard;
//     }
//     // Otherwise just let them pick the card
//     else if (typeof x == "number") {
//         let pickedSuit = Math.floor(x / 13);
//         return { suit: suits[pickedSuit], card: x % 13 };
//     }
// }

// let myDeck = [{ suit: "diamonds", card: 2 }, { suit: "spades", card: 10 }, { suit: "hearts", card: 4 }];
// let pickedCard1 = myDeck[pickCard(myDeck)];
// alert("card: " + pickedCard1.card + " of " + pickedCard1.suit);

// let pickedCard2 = pickCard(15);
// alert("card: " + pickedCard2.card + " of " + pickedCard2.suit);


let suits = ["hearts", "spades", "clubs", "diamonds"];

function pickCard(x: {suit: string; card: number; }[]): number;
function pickCard(x: number): {suit: string; card: number; };
function pickCard(x: any): any {
    // Check to see if we're working with an object/array
    // if so, they gave us the deck and we'll pick the card
    if (typeof x == "object") {
        let pickedCard = Math.floor(Math.random() * x.length);
        return pickedCard;
    }
    // Otherwise just let them pick the card
    else if (typeof x == "number") {
        let pickedSuit = Math.floor(x / 13);
        return { suit: suits[pickedSuit], card: x % 13 };
    }
}

let myDeck = [{ suit: "diamonds", card: 2 }, { suit: "spades", card: 10 }, { suit: "hearts", card: 4 }];
let pickedCard1 = myDeck[pickCard(myDeck)];
alert("card: " + pickedCard1.card + " of " + pickedCard1.suit);

let pickedCard2 = pickCard(15);
alert("card: " + pickedCard2.card + " of " + pickedCard2.suit);